package com.kensure.shike.cj.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.shike.cj.dao.CJTitleDao;
import com.kensure.shike.cj.model.CJBaoming;
import com.kensure.shike.cj.model.CJTitle;
import com.kensure.shike.user.model.SKUser;


/**
 * 抽奖主表服务实现类
 * @author fankd created on 2019-11-13
 * @since 
 */
@Service
public class CJTitleService extends JSBaseService{
	
	@Resource
	private CJTitleDao dao;
	@Resource
	private CJBaomingService cJBaomingService;
    
    public CJTitle selectOne(Long id){
    	return dao.selectOne(id);
    }
	
	public List<CJTitle> selectByIds(Collection<Long> ids){
		return dao.selectByIds(ids);
	}
	
	public List<CJTitle> selectAll(){
		return dao.selectAll();
	}
	
	public List<CJTitle> selectByWhere(Map<String, Object> parameters){
		return dao.selectByWhere(parameters);
	}
	
	
	public long selectCount(){
		return dao.selectCount();
	}
	
	public long selectCountByWhere(Map<String, Object> parameters){
		return dao.selectCountByWhere(parameters);
	}
	
	
	public boolean insert(CJTitle obj){
		super.beforeInsert(obj);
		return dao.insert(obj);
	}
	
	public boolean update(CJTitle obj){
		super.beforeUpdate(obj);
		return dao.update(obj);
	}
    
    public boolean updateByMap(Map<String, Object> params){
		return dao.updateByMap(params);
	}
    
    
	public boolean delete(Long id){
		return dao.delete(id);
	}	
	
    public boolean deleteMulti(Collection<Long> ids){
		return dao.deleteMulti(ids);
	}
    
    public boolean deleteByWhere(Map<String, Object> parameters){
		return dao.deleteByWhere(parameters);
	}
    
    /**
     * 后台用
     * @param page
     * @return
     */
    public List<CJTitle> selectByWhere(PageInfo page) {
		Map<String, Object> parameters = MapUtils.genMap("orderby", "id");
		MapUtils.putPageInfo(parameters, page);
		return selectByWhere(parameters);
	}
  

    /**
     * 前台已经开启的抽奖活动
     * @param typeid
     * @return
     */
    public List<CJTitle> selectByStart() {
		Map<String, Object> parameters = MapUtils.genMap("status", 9, "orderby", "id");
		return selectByWhere(parameters);
	}
    
    /**
     * 前台首页是否弹出抽奖
     * @return
     */
    public CJTitle selectIndex(SKUser user) {
		Map<String, Object> parameters = MapUtils.genMap("status", 9, "orderby", "id");
		List<CJTitle> list = selectByWhere(parameters);
		CJTitle t = null;
		if(CollectionUtils.isNotEmpty(list)){
			t = list.get(0);
			List<CJBaoming> bmlist = cJBaomingService.selectByUser(user.getId(), t.getId());
			if(CollectionUtils.isNotEmpty(bmlist)){
				t = null;
			}		
		}
		return t;
	}
    
    
    /**
     * webview判断抽奖情况
     * @return
     */
    public CJTitle selectChouJiang(SKUser user) {
		Map<String, Object> parameters = MapUtils.genMap("status", 9, "orderby", "id");
		List<CJTitle> list = selectByWhere(parameters);
		CJTitle t = null;
		if(CollectionUtils.isNotEmpty(list)){
			t = list.get(0);
			List<CJBaoming> bmlist = cJBaomingService.selectByUser(user.getId(), t.getId());
			if(CollectionUtils.isNotEmpty(bmlist)){
				t.setBaoming(bmlist.get(0));
			}		
		}
		return t;
	}
    
    
    
    /**
     * 修改状态
     * @return
     */
    public void updateStatus(Long id,Integer status) {
		CJTitle obj = selectOne(id);
		obj.setStatus(status);
		update(obj);
	}
}
